package com.imageim.yimirror.ui.widght;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Bruce.Zhou on 2018/6/11 13:58.
 * Email: 907160968@qq.com
 * 错误提示页面
 */

public class EmptyView extends LinearLayout {

    @BindView(R.id.iv_error_img)
    ImageView mIvErrorImg;
    @BindView(R.id.tv_error_text)
    TextView mTvErrorText;
    @BindView(R.id.btn_finish)
    Button mBtnFinish;
    private View mView;
    private Unbinder mUnbinder;

    public EmptyView(Context context) {
        super(context);
        init(context);
    }

    public EmptyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.empty_view, this);
        mUnbinder = ButterKnife.bind(this, mView);
    }

    /**
     * 设置错误提示图片
     *
     * @param imgResId 图片资源ID
     */
    public void setErrorImg(int imgResId) {
        mIvErrorImg.setImageResource(imgResId);
    }

    /**
     * 设置错误提示文字
     *
     * @param errorText 错误提示文字内容
     */
    public void setErrorContent(String errorText) {
        if (!TextUtils.isEmpty(errorText)) {
            mTvErrorText.setText(errorText);
        }
    }

    /**
     * 设置错误提示文字颜色
     *
     * @param color 解析后的RGB
     *              示例：Color.parse("#fffff")
     */
    public void setErrorContentColor(int color) {
        mTvErrorText.setTextColor(color);
    }

    /**
     * 设置完成按钮内容
     *
     * @param errorBtnContent 文字内容
     */
    public void setBtnContent(String errorBtnContent) {
        mBtnFinish.setText(errorBtnContent);
    }

    /**
     * 关闭或者刷新按钮是否可见
     *
     * @param visiable
     */
    public void setBtnVisiable(boolean visiable) {
        if (visiable) {
            if (mBtnFinish != null) {
                mBtnFinish.setVisibility(View.VISIBLE);
            }
        } else {
            if (mBtnFinish != null) {
                mBtnFinish.setVisibility(View.GONE);
            }
        }
    }


    @OnClick(R.id.btn_finish)
    public void onViewClicked(View v) {
        if (v.getId() == R.id.btn_finish) {
            if (mOnFinishBtnClickListener != null) {
                mOnFinishBtnClickListener.onFinishBtnClick();
            }
        }
    }

    public interface OnFinishBtnClickListener {

        void onFinishBtnClick();
    }

    public void setOnFinishBtnClickListener(OnFinishBtnClickListener onFinishBtnClickListener) {
        this.mOnFinishBtnClickListener = onFinishBtnClickListener;

    }

    private OnFinishBtnClickListener mOnFinishBtnClickListener;

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
